package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TMemberRecommend;
import com.quwan.manager.service.MemberRecommendService;
import com.quwan.mapper.TMemberRecommendMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author laozhang
 * @date 2022/02/16
 */
@Service
@RequiredArgsConstructor
public class MemberRecommendServiceImpl implements MemberRecommendService {
    private final TMemberRecommendMapper memberRecommendMapper;

    @Override
    public PageResult<TMemberRecommend> getMemberRecommendListPage(int page, int size, String realName, String nickName, String beginTime, String endTime) {
        LambdaQueryWrapper<TMemberRecommend> wrapper = new LambdaQueryWrapper<TMemberRecommend>()
                .orderByAsc(TMemberRecommend::getSort);
        if (StringUtils.isNotBlank(realName)) {
            wrapper.like(TMemberRecommend::getRealName, realName);
        }
        if (StringUtils.isNotBlank(nickName)) {
            wrapper.like(TMemberRecommend::getNickName, nickName);
        }
        if (StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberRecommend::getAddTime, beginTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberRecommend::getAddTime, endTime + " 23:59:59");
        }

        Page<TMemberRecommend> memberRecommendPage = memberRecommendMapper.selectPage(new Page<>(page, size), wrapper);
        List<TMemberRecommend> reslist = memberRecommendPage.getRecords();
        PageResult<TMemberRecommend> result = new PageResult<TMemberRecommend>()
                .setCurrent(memberRecommendPage.getCurrent())
                .setPages(memberRecommendPage.getPages())
                .setSize(memberRecommendPage.getSize())
                .setTotal(memberRecommendPage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<TMemberRecommend> getMemberRecommendList(String realName, String nickName, String beginTime, String endTime) {
        LambdaQueryWrapperX<TMemberRecommend> wrapper = new LambdaQueryWrapperX<TMemberRecommend>()
                .orderByDesc(TMemberRecommend::getSort);
        if (StringUtils.isNotBlank(realName)) {
            wrapper.like(TMemberRecommend::getRealName, realName);
        }
        if (StringUtils.isNotBlank(nickName)) {
            wrapper.like(TMemberRecommend::getNickName, nickName);
        }
        if (StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberRecommend::getAddTime, beginTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberRecommend::getAddTime, endTime + " 23:59:59");
        }
        List<TMemberRecommend> reslist = memberRecommendMapper.selectList(wrapper);
        return reslist;
    }

    @Transactional
    @Override
    public boolean addMemberRecommend(TMemberRecommend recommend) {
        memberRecommendMapper.delete(new QueryWrapper<>(new TMemberRecommend().setMId(recommend.getMId())));
        return memberRecommendMapper.insert(recommend) == 1;
    }

    @Override
    public boolean delMemberRecommend(Integer id) {
        return memberRecommendMapper.deleteById(id) == 1;
    }


}
